package com.masterclient.app.modules.goods.adapter;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.masterclient.app.R;
import com.masterclient.app.base.MainViewHolder;
import com.eoner.baselibrary.bean.goods.CommodityActivityMessage;
import com.eoner.baselibrary.bean.goods.CommodityDetailData;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.baselibrary.utils.TimeUtil;
import com.eoner.baselibrary.utils.Utils;
import com.eoner.baselibrary.utils.ViewTransformUtil;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class CommodityDetailFansTimeLimitAdapter extends DelegateAdapter.Adapter<MainViewHolder> {

    Activity mContext;

    private LayoutHelper mLayoutHelper;


    private VirtualLayoutManager.LayoutParams mLayoutParams;
    private int mCount = 0;

    CommodityDetailData data;


    public CommodityDetailFansTimeLimitAdapter(Activity context, LayoutHelper layoutHelper, int count) {
        this(context, layoutHelper, count, null, null);
    }

    public CommodityDetailFansTimeLimitAdapter(Activity context, LayoutHelper layoutHelper, int count,
                                               @NonNull VirtualLayoutManager.LayoutParams layoutParams, CommodityDetailData data) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.mCount = count;
        this.mLayoutParams = layoutParams;
        this.data = data;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @Override
    protected void onBindViewHolderWithOffset(MainViewHolder holder, int position, int offsetTotal) {

    }

    @Override
    public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return null;
    }

    @Override
    public void onBindViewHolder(MainViewHolder holder, int position) {
        if (mLayoutParams != null) {
            holder.itemView.setLayoutParams(
                    new VirtualLayoutManager.LayoutParams(mLayoutParams));
        }

        View time_activity_layout = holder.itemView.findViewById(R.id.time_activity_layout);
        ImageView imageView = holder.itemView.findViewById(R.id.image);
        TextView status = holder.itemView.findViewById(R.id.tv_activity_state);
        TextView tv_hour = holder.itemView.findViewById(R.id.tv_hour);
        TextView tv_millisecond = holder.itemView.findViewById(R.id.tv_millisecond);
        Utils.setDin(tv_hour,mContext);
        Utils.setDin(tv_millisecond,mContext);
        TextView tv_activity_txt = holder.itemView.findViewById(R.id.tv_activity_txt);

        CommodityActivityMessage commodityActivityMessage = data.getSingle_promotion();
        if (commodityActivityMessage == null) {
            imageView.setVisibility(View.GONE);
            time_activity_layout.setVisibility(View.GONE);
            tv_activity_txt.setVisibility(View.GONE);
        } else {
            imageView.setVisibility(View.VISIBLE);
            int height = (int) (ScreenUtils.getScreenWidth() * 0.16);
            if (commodityActivityMessage.getType() != null && commodityActivityMessage.getType().equals("image")) {
                ViewTransformUtil.glideImageView(mContext, commodityActivityMessage.getLeftTitle(),
                        imageView, R.color.transparent, ScreenUtils.getScreenWidth(), height);
            }
            if (TimeUtil.isBeforeNow(commodityActivityMessage.getStart_at()) && !TimeUtil.isBeforeNow(commodityActivityMessage.getEnd_at())) {
                //粉丝福利抢购中
                long subTime = Integer.parseInt(commodityActivityMessage.getSub_time()) * 60 * 60 * 1000;
                if (TimeUtil.timeRemain2(commodityActivityMessage.getEnd_at()) > subTime) {
                    //粉丝福利抢购中---大于72小时
                    tv_activity_txt.setText(commodityActivityMessage.getOngoing_gt());
                    time_activity_layout.setVisibility(View.GONE);
                    tv_activity_txt.setVisibility(View.VISIBLE);
                    long amount = ((TimeUtil.timeRemain2(commodityActivityMessage.getEnd_at()) - subTime) / 1000);
                    Observable.interval(0, 1, TimeUnit.SECONDS, Schedulers.computation())
                            .take(amount).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Long>() {
                        @Override
                        public void onSubscribe(Disposable d) {

                        }

                        @Override
                        public void onNext(Long aLong) {

                        }

                        @Override
                        public void onError(Throwable e) {
                        }

                        @Override
                        public void onComplete() {
                            notifyDataSetChanged();
                        }
                    });
                } else {
                    //粉丝福利抢购中---小于72小时
                    time_activity_layout.setVisibility(View.VISIBLE);
                    tv_activity_txt.setVisibility(View.GONE);
                    status.setText(commodityActivityMessage.getOngoing());
                    long amount = (TimeUtil.timeRemain2(commodityActivityMessage.getEnd_at()) / 1000);
                    long amount2 = (TimeUtil.timeRemain2(commodityActivityMessage.getEnd_at()));
                    Observable.interval(0, 10, TimeUnit.MILLISECONDS, Schedulers.computation())
                            .take(amount).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Long>() {
                        @Override
                        public void onSubscribe(Disposable d) {

                        }

                        @Override
                        public void onNext(Long aLong) {
                            TimeUtil.showTimeWithMsStr(amount2 - aLong * 10, tv_hour, tv_millisecond);
                        }

                        @Override
                        public void onError(Throwable e) {
                        }

                        @Override
                        public void onComplete() {
                            notifyDataSetChanged();
                            time_activity_layout.setVisibility(View.GONE);
                            tv_activity_txt.setVisibility(View.GONE);
                        }
                    });
                }
            } else if (!TimeUtil.isBeforeNow(commodityActivityMessage.getStart_at())) {
                //粉丝福利未开始
                long subTime = Integer.parseInt(commodityActivityMessage.getSub_time()) * 60 * 60 * 1000;
                if (TimeUtil.timeRemain2(commodityActivityMessage.getStart_at()) > subTime) {
                    //粉丝福利未开始---大于72小时
                    tv_activity_txt.setText(commodityActivityMessage.getWaiting_gt());
                    time_activity_layout.setVisibility(View.GONE);
                    tv_activity_txt.setVisibility(View.VISIBLE);
                    long amount = ((TimeUtil.timeRemain2(commodityActivityMessage.getStart_at()) - subTime) / 1000);
                    Observable.interval(0, 1, TimeUnit.SECONDS, Schedulers.computation())
                            .take(amount).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Long>() {
                        @Override
                        public void onSubscribe(Disposable d) {

                        }

                        @Override
                        public void onNext(Long aLong) {

                        }

                        @Override
                        public void onError(Throwable e) {
                        }

                        @Override
                        public void onComplete() {
                            notifyDataSetChanged();
                        }

                    });
                } else {
                    //粉丝福利未开始---小于72小时
                    time_activity_layout.setVisibility(View.VISIBLE);
                    tv_activity_txt.setVisibility(View.GONE);
                    status.setText(commodityActivityMessage.getWaiting());
                    long amount = (TimeUtil.timeRemain2(commodityActivityMessage.getStart_at()) / 1000);
                    long amount2 = (TimeUtil.timeRemain2(commodityActivityMessage.getStart_at()));
                    Observable.interval(0, 10, TimeUnit.MILLISECONDS, Schedulers.computation())
                            .take(amount).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Long>() {
                        @Override
                        public void onSubscribe(Disposable d) {

                        }

                        @Override
                        public void onNext(Long aLong) {
                            TimeUtil.showTimeWithMsStr(amount2 - aLong * 10, tv_hour, tv_millisecond);
                        }

                        @Override
                        public void onError(Throwable e) {
                        }

                        @Override
                        public void onComplete() {
                            notifyDataSetChanged();
                            time_activity_layout.setVisibility(View.GONE);
                            tv_activity_txt.setVisibility(View.GONE);
                        }
                    });
                }
            } else {
                tv_activity_txt.setText(commodityActivityMessage.getOver());
                time_activity_layout.setVisibility(View.GONE);
                tv_activity_txt.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public int getItemCount() {
        return mCount;
    }

    @Override
    public int getItemViewType(int position) {
        return 1;
    }
}
